package com.security.system.controller;

import com.security.system.service.ExportExcelService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalTime;

@RestController
@RequestMapping("excel")
public class ExportExcelController {

    @Resource
    ExportExcelService service;

    @PostMapping("")
    public void get(HttpServletResponse response){
        try {
            Workbook wb = service.exportExcel();
            //获取 当前时间 格式：年月日时分秒
            LocalTime time = LocalTime.now();
            String strdate = LocalDate.now().toString().replace("-","");
            String times = time.getHour()+""+time.getMinute()+""+time.getSecond()+"";
            //编辑 Excel 的 文件名
            String fileName = "users - "  + strdate + times + ".xls";
            // axios 请求 配置 response.setContentType("application/vnd.ms-excel");
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(),"iso-8859-1"));
            response.flushBuffer();
            wb.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
